// Copyright (c) 2022, hzmes and contributors
// For license information, please see license.txt

frappe.ui.form.on('SRM Sample RequestCaigou', {
	refresh: function(frm) {
		if (!frm.is_new() && frm.doc.docstatus == 1) {
			frm.add_custom_button(__("New SRM Sample Admit"), function () {
				let admit = frappe.model.get_new_doc("SRM Sample Admit");
				admit.requestcaigou = frm.doc.name;
				admit.sample_category_purchase = frm.doc.material_name;
				admit.sample_model = frm.doc.material_model;
				admit.supplier = frm.doc.current_supplier_number;
				admit.sample_qty = frm.doc.qty
				frappe.set_route("Form", admit.doctype, admit.name);
			}).addClass('btn-primary');
		}
		// 给供应商列表中的邮箱发送邮件
		if (1 == 1) {
			frm.add_custom_button("发送邮件", () => {
				if (frm.doc.supplier_subs) {
					if (frm.doc.admit_closed == 1) {
						frappe.confirm('已经发送, 需要重新发送一次?',
							() => {
								// action to perform if Yes is selected
								// 链式操作:先把当前子表中的email遍历(如果某一行有多个email地址并且是用,号分隔的)出来；再toString()组合为字符串;再转换为列表
								var rev_emails = cur_frm.doc.supplier_subs.map((item) =>{return item.email.split(',')}).toString().split(",")
								if (rev_emails) {
									frm.call({
										method: "send_supplier_emails",
										doc: frm.doc,
										args: {
											rev_email: rev_emails,
										},
										freeze: true,
										freeze_message: __("running..."),
										callback: function(r) {
											frappe.msgprint("已经安排邮件发送队列!");
										}
									})
								}
							}, () => {
								// action to perform if No is selected
							})
					}
					else {
						// 链式操作:先把当前子表中的email遍历(如果某一行有多个email地址并且是用,号分隔的)出来；再toString()组合为字符串;再转换为列表
						var rev_emails = cur_frm.doc.supplier_subs.map((item) =>{return item.email.split(',')}).toString().split(",")
						if (rev_emails) {
							frm.call({
								method: "send_supplier_emails",
								doc: frm.doc,
								args: {
									rev_email: rev_emails,
								},
								freeze: true,
								freeze_message: __("running..."),
								callback: function(r) {
									frappe.msgprint("已经安排邮件发送队列!");
								}
							})
						}
					}
				}
				else {
					frappe.msgprint("供应商收件人不能为空!")
				}
			})
		};
	},

	setup: function(frm) {
		

		frm.set_query('supplier', 'supplier_subs', function(doc, cdt, cdn) {
			const rorw = locals[cdt][cdn];
			return {
				'filters': { managestatus: ['in', ['四级', '三级', '二级']] }
			}
		});
		
		// frm.set_query('material_company', function(doc) {
		// 	return {
		// 		'filters': { 'group': '内部供应商'}
		// 	}
		// });
		
		// // 配置默认值为GYN0001，即广东九联公司
		// if (!frm.doc.material_company) {
		// 	frm.set_value("material_company", "GYN0001");
		// }
	},

	before_save: function(frm) {
		if (!frm.doc.current_supplier || !frm.doc.current_supplier_number) {
			frm.set_value({current_supplier_number: frm.doc.supplier_subs[0].supplier, current_supplier: frm.doc.supplier_subs[0].supplier_name})
		}
	},

});

frappe.ui.form.on('SRM Sample RequestCaigou Sub', {
	// refresh: function(frm) {

	// }

	supplier: function(frm, cdt, cdn) {
		let row = locals[cdt][cdn];
		let email_get = '';
		let r = frappe.db.get_list('SRM Supplier Contact Sub',
                   {filters:{'parent': row.supplier}, fields:['email_address'], as_list:true}
			).then( (r) => {
				if (r) {
					email_get = r.join(",");
					// console.log(r);
				}
			}).then ( () => {
				// console.log(b + '-' + email_get);
				row.email = email_get;
				frm.refresh_field('supplier_subs');
			})
	}
});